<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function func_html_href_link($link, $param = '', $ssl = 'NOSSL', $no_prefix = false) {
	global $SETTINGS;
	if( $ssl == 'SSL' && $SETTINGS['sslenabled'] == 1 ) {
		$url = (($no_prefix == false) ? CONFIG_PROTOCOL_HTTPS . CONFIG_URL : '') . $link;
	} else {
		$url = (($no_prefix == false) ? CONFIG_PROTOCOL_HTTP . CONFIG_URL : '') . $link;
	}
	if( $param != '' ) {
		$url .= '?' . $param;
	}
	
	return $url;
}

// The HTML image wrapper function
function func_html_image($src, $alt = '', $width = '', $height = '', $parameters = '') {
	if ( (empty($src) || ($src == DIR_WS_IMAGES)) && (IMAGE_REQUIRED == 'false') ) {
		return false;
	}
	
	// alt is added to the img tag even if it is null to prevent browsers from outputting
	// the image filename as default
	$image = '<img src="' . util_string_output($src) . '" border="0" alt="' . util_string_output($alt) . '"';
	
	if (util_not_null($alt)) {
		$image .= ' title=" ' . util_string_output($alt) . ' "';
	}

	if ( (CONFIG_CALCULATE_IMAGE_SIZE == 'true') && (empty($width) || empty($height)) ) {
		if ($image_size = @getimagesize($src)) {
			if (empty($width) && util_not_null($height)) {
				$ratio = $height / $image_size[1];
				$width = intval($image_size[0] * $ratio);
			} elseif (util_not_null($width) && empty($height)) {
				$ratio = $width / $image_size[0];
				$height = intval($image_size[1] * $ratio);
			} elseif (empty($width) && empty($height)) {
				$width = $image_size[0];
				$height = $image_size[1];
			}
		} elseif (IMAGE_REQUIRED == 'false') {
			return false;
		}
	}
	
	if (util_not_null($width) && util_not_null($height)) {
		$image .= ' width="' . util_string_output($width) . '" height="' . util_string_output($height) . '"';
	}
	
	if (util_not_null($parameters)) $image .= ' ' . $parameters;
	
	$image .= '>';
	
	return $image;
}

// The HTML form submit button wrapper function
// Outputs a button in the selected language
function func_html_image_submit($image, $alt = '', $parameters = '') {
	global $language;
	
	$image_submit = '<input type="image" src="' . util_string_output(DIR_WS_LANGUAGES . $language . '/images/buttons/' . $image) . '" border="0" alt="' . util_string_output($alt) . '"';
	
	if (util_not_null($alt)) $image_submit .= ' title=" ' . util_string_output($alt) . ' "';
	
	if (util_not_null($parameters)) $image_submit .= ' ' . $parameters;
	
	$image_submit .= '>';
	
	return $image_submit;
}

// Output a function button in the selected language
function func_html_image_button($image, $alt = '', $parameters = '') {
	global $language;
	
	return func_html_image(DIR_WS_LANGUAGES . $language . '/images/buttons/' . $image, $alt, '', '', $parameters);
}

// Output a separator either through whitespace, or with an image
function func_html_draw_separator($image = 'pixel_black.gif', $width = '100%', $height = '1') {
	return func_html_image(DIR_WS_IMAGES . $image, '', $width, $height);
}

// Output a form
function func_html_draw_form($name, $action, $method = 'post', $parameters = '') {
	$form = '<form name="' . util_string_output($name) . '" action="' . util_string_output($action) . '" method="' . util_string_output($method) . '"';
	
	if (util_not_null($parameters)) $form .= ' ' . $parameters;
	
	$form .= '>';
	
	return $form;
}

// Output a form input field
function func_html_draw_input_field($name, $value = '', $parameters = '', $type = 'text', $reinsert_value = true) {
	$field = '<input type="' . util_string_output($type) . '" name="' . util_string_output($name) . '"';
	
	if ( (isset($GLOBALS[$name])) && ($reinsert_value == true) ) {
		$field .= ' value="' . util_string_output(stripslashes($GLOBALS[$name])) . '"';
	} elseif (util_not_null($value)) {
		$field .= ' value="' . util_string_output($value) . '"';
	}
	
	if (util_not_null($parameters)) $field .= ' ' . $parameters;
	
	$field .= '>';
	
	return $field;
}

// Output a form password field
function func_html_draw_password_field($name, $value = '', $parameters = 'maxlength="40"') {
	return func_html_draw_input_field($name, $value, $parameters, 'password', false);
}

// Output a selection field - alias function for func_html_draw_checkbox_field() and func_html_draw_radio_field()
function func_html_draw_selection_field($name, $type, $value = '', $checked = false, $parameters = '') {
	$selection = '<input type="' . util_string_output($type) . '" name="' . util_string_output($name) . '"';
	
	if (util_not_null($value)) $selection .= ' value="' . util_string_output($value) . '"';
	
	if ( ($checked == true) || ( isset($GLOBALS[$name]) && is_string($GLOBALS[$name]) && ( ($GLOBALS[$name] == 'on') || (isset($value) && (stripslashes($GLOBALS[$name]) == $value)) ) ) ) {
		$selection .= ' CHECKED';
	}
	
	if (util_not_null($parameters)) $selection .= ' ' . $parameters;
	
	$selection .= '>';
	
	return $selection;
}

// Output a form checkbox field
function func_html_draw_checkbox_field($name, $value = '', $checked = false, $parameters = '') {
	return func_html_draw_selection_field($name, 'checkbox', $value, $checked, $parameters);
}

// Output a form radio field
function func_html_draw_radio_field($name, $value = '', $checked = false, $parameters = '') {
	return func_html_draw_selection_field($name, 'radio', $value, $checked, $parameters);
}

// Output a form textarea field
function func_html_draw_textarea_field($name, $wrap, $width, $height, $text = '', $parameters = '', $reinsert_value = true) {
	$field = '<textarea name="' . util_string_output($name) . '" wrap="' . util_string_output($wrap) . '" cols="' . util_string_output($width) . '" rows="' . util_string_output($height) . '"';
	
	if (util_not_null($parameters)) $field .= ' ' . $parameters;
	
	$field .= '>';
	
	if ( (isset($GLOBALS[$name])) && ($reinsert_value == true) ) {
		$field .= util_string_output_protected(stripslashes($GLOBALS[$name]));
	} elseif (util_not_null($text)) {
		$field .= util_string_output_protected($text);
	}
	
	$field .= '</textarea>';
	
	return $field;
}

// Output a form hidden field
function func_html_draw_hidden_field($name, $value = '', $parameters = '') {
	$field = '<input type="hidden" name="' . util_string_output($name) . '"';
	
	if (util_not_null($value)) {
		$field .= ' value="' . util_string_output($value) . '"';
	} elseif (isset($GLOBALS[$name])) {
		$field .= ' value="' . util_string_output(stripslashes($GLOBALS[$name])) . '"';
	}
	
	if (util_not_null($parameters)) $field .= ' ' . $parameters;
	
	$field .= '>';
	
	return $field;
}

// Hide form elements
function func_html_hide_session_id() {
	global $session_started, $SID;
	
	if (($session_started == true) && util_not_null($SID)) {
		return func_html_draw_hidden_field(func_html_session_name(), func_html_session_id());
	}
}

// Output a form pull down menu
function func_html_draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
	$field = '<select name="' . util_string_output($name) . '"';
	
	if (util_not_null($parameters)) $field .= ' ' . $parameters;
	
	$field .= '>';
	
	if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);
	
	for($i=0, $n=sizeof($values); $i<$n; $i++) {
		$field .= '<option value="' . util_string_output($values[$i]['id']) . '"';
		if ($default == $values[$i]['id'] ) {
			$field .= ' SELECTED';
		}
		
		$field .= '>' . util_string_output($values[$i]['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
	}
	$field .= '</select>';
	
	if ($required == true) $field .= CONST_TEXT_FIELD_REQUIRED;
	
	return $field;
}

// Creates a pull-down list of countries
function func_html_get_country_list($name, $selected = '', $parameters = '') {
	$countries_array = array(array('id' => '', 'text' => PULL_DOWN_DEFAULT));
	$countries = func_html_get_countries();
	
	for($i=0, $n=sizeof($countries); $i<$n; $i++) {
		$countries_array[] = array('id' => $countries[$i]['countries_id'], 'text' => $countries[$i]['countries_name']);
	}
	
	return func_html_draw_pull_down_menu($name, $countries_array, $selected, $parameters);
}
//
?>